#include <stdio.h>
#include<stdlib.h>
#include <stdbool.h>
#define InitSize 10
typedef struct{
	int *data;
	int length;
	int MaxSize;
}SqList;
void InitList(SqList *list,int size){//初始化。为传过来的结构体开辟内存空间
//	printf("%d",list->length);
	list->data = (int *)malloc(sizeof(int) * size);
	if(list->data==NULL)//判断空间是否开辟成功
	{
		printf("malloc fail\n");
		exit(0);
	}
	list->length = 0;
	list->MaxSize = InitSize;
}
void ListInsert(){
	
}
void listOutput(SqList *list){
	printf("顺序表的长度为:%d\n",list->length);
	for(int i=0;i<list->length;i++){
		printf("%d\n",list->data[i]);
	}
}
void ListInput(SqList *list){
	printf("请输入你要输入的数字,-1以结束输入!\n");
	int data,i;
	i = 0;
	while(scanf("%d",&data) && data!=-1){
		list->data[list->length++] = data;
	}
	
}
void ListDelete(SqList *list,int index,int *e){
	if(index<0||index>list->length-1){
		printf("出错了！");
		return;
	}
	*e = list->data[index];//将要删除的值保存到e指向的空间里面
	for(int i= index;i<list->length;i++){
		list->data[i] = list->data[i+1];
	}
	list->length--;
	return;
}
bool ListCombine(SqList *list1,SqList *list2,SqList *list){
	//先给新的顺序表分配length1+length2的数组空间
	InitList(list,list1->length+list2->length);
	int i = 0, j = 0,k = 0;
	//这里假设都是顺序表是从小到大排列
	while (i < list1->length && j < list2->length) {
		if(list1->data[i]<=list2->data[j]){
			list->data[k++] = list1->data[i++];
		}
		else{
			list->data[k++] = list2->data[j++];
		}
		
	}
	while(i < list1->length){
		list->data[k++] = list1->data[i++];
	}
	while(j < list2->length){
		list->data[k++] = list2->data[j++];
	}
	list->length = k;
	return true;
}
int main() {
	SqList *list1 = (SqList*) malloc(sizeof(SqList));
	SqList *list2 = (SqList*) malloc(sizeof(SqList));
	SqList *list = (SqList*) malloc(sizeof(SqList));
	InitList(list1,InitSize);
	printf("请输入表一的数据:\n");
	ListInput(list1);
	InitList(list2,InitSize);
	printf("请输入表二的数据:\n");
	ListInput(list2);
	printf("表一的数据为:\n");
	listOutput(list1);
	printf("表二的数据为:\n");
	listOutput(list2);
	printf("开始合并.....\n");
	if(ListCombine(list1, list2, list)){
		printf("合并成功！合并后的顺序表为:\n");
		listOutput(list);
	}
	else {
		printf("操作失败!\n");
	}
}